/*
 * @Date: 2021-02-02 15:54:05
 * @LastEditors: E'vils
 * @LastEditTime: 2021-02-04 14:00:38
 * @Description: 
 * @FilePath: /src/pages/tablePreview/tablePreview.js
 */
/* eslint-disable no-undef */
//index.js
//获取应用实例
const app = getApp();
import config from "../../config";
import {shareTableInfo,inputtable} from "../../utils/api/user";
import dayjs from "../../utils/dayjs/dayjs.min";

Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        displayArea: app.globalData.displayArea,
        ImgUrl: app.globalData.ImgUrl,
        colorList: config.colorList,
        gradeList: app.globalData.gradeList,
        weeksSwitch: false,
        semester: null,
        nowM:1,  // 当前月份
        showWeek: 8, // 显示周
        currentWeek: 8, // 当前周
        weeksList: [], // 当前周日期列表
        editTableId: null,
        classTime: null,
        courseInfo: [], // 自己课表
        havcourse:false, // 全部课程是有的
        checkbox: [],
    },

    // 上课节数
    courseNum() {
        let checkbox = [];
        for (let index = 1; index <= 20; index++) {
            checkbox.push({
                value: index,
                checked: false,
            });
        }
        this.setData({
            checkbox,
        });
    },
    /**
     * 生成当前周的日期列表
     */
    weeksList(day) {
        let todayWeek = day.format("d") ? dayjs().format("d") - 1 : 6;
        let Fist = day.subtract(todayWeek, "day");
        let _week = [];
        for (let index = 0; index < 7; index++) {
            _week.push(Fist.add(index, "day").format("MM/DD"));
        }
        this.setData({
            weeksList: _week,
            todayWeek,
        });
    },
    /**
     * 计算课程表是否符合当前周
     * @param {*} week 显示周
     * @param {*} userInfo 数据
     */
    weekCourseInfo(week, userInfo) {
        if (!userInfo) {
            return null;
        } else {
            this.setData({
                havcourse:userInfo.length > 0
            });
            let courseInfo = userInfo
                .map((v) => {
                    let attend = v.attend.split(",").filter((e) => e == week);
                    if (attend[0]) {
                        return v;
                    }
                })
                .filter((v) => v);
            return courseInfo.length > 0 ? courseInfo : null;
        }
    },
    /**
     * 设置课程表
     */
    setcourse(week, shareCourse = this.data.shareCourse ) {
        // 本人课表
        let _courseInfo =
        shareCourse ?
            JSON.parse(shareCourse) :
            null;
        let courseInfo = this.weekCourseInfo(week, _courseInfo);
        this.setData({
            courseInfo,
        });
    },
    onLoad(query){
        console.log("query", query);
        let scene = query.scene && query.scene.split("%3D");
        console.log(query,scene,scene[1], "query");
        this.setData({
            shareId:scene[1]
        });
    },
    onShow(){
        let currentWeek = wx.getStorageSync("currentWeek");
        this.courseNum();
        this.weeksList(dayjs());
        let semester = null; // 当前是第几学期
        let nowM = dayjs().format("M"); // 获取现在的月份
        semester = nowM < 3 || nowM > 9 ? 1 : 2;
        this.setData({
            semester,
            nowM,
            currentWeek,
            showWeek: currentWeek,
        });
        if (!this.data.classTime) {
            let classTime = wx.getStorageSync("classTime");
            this.setData({
                classTime,
                currentWeek,
                showWeek: currentWeek,
            });
        }
        shareTableInfo({
            u:this.data.shareId
        }).then(res=>{
            console.log("res", res);
            this.setData({
                shareCourse:res.data
            });
            this.setcourse(currentWeek,res.data);
        });
    },
    /**
     * 后退一页
     */
    BackPage() {
        wx.navigateBack({
            delta: 1,
        });
    },
    /**
     * 取消绑定弹窗
     */
    removeBind() {
        this.setData({
            removeBind: true,
        });
    },
    /**
     * 回到当前周
     */
    backToCurrent() {
        this.setData({
            showWeek: this.data.currentWeek,
            weeksSwitch: false,
        });
        this.weeksList(dayjs());
        let currentWeek = wx.getStorageSync("currentWeek");
        this.setcourse(currentWeek);
    },
    /**
     * 选择周
     */
    ChooseCheckbox(e) {
        if (!app.globalData.userInfo) {
            return;
        }
        let value = e.currentTarget.dataset.value;
        let diff = value - this.data.currentWeek;
        this.setData({
            showWeek: value,
        });
        this.setcourse(value);
        if (diff == 0) {
            // 没有周数切换
            return;
        } else if (diff > 0) {
            let _add = dayjs().add(diff, "week");
            this.weeksList(_add);
        } else {
            let _sub = dayjs().subtract(-diff, "week");
            this.weeksList(_sub);
        }
    },
    /**
     * 周数切换
     */
    weeksSwitch() {
        this.setData({
            weeksSwitch: true,
        });
    },
    hideweeksSwitch() {
        this.setData({
            weeksSwitch: false,
        });
    },
    /**
     * 确认导入课表
    */
    importSchedule(){
        wx.showModal({
            title: "温馨提示",
            content: "导入课表将覆盖已有课表,是否导入?",
            success(res) {
                if (res.confirm) {
                    inputtable({
                        user_id:this.data.shareId
                    }).then(v=>{
                        console.log("v", v);
                    });
                } else if (res.cancel) {
                    return;
                }
            },
        });
        console.log("importSchedule",);
    },
    /**
     * 取消导入课表
    */
    cancelImport(){
        wx.switchTab({
            url: "/pages/index/index",
        });
    },
});
